package com.android.canvasproject.view.fragments;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Fragment;
import android.content.Context;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.canvasproject.R;
import com.android.canvasproject.enums.CanvasTypeEnum;
import com.android.canvasproject.model.dao.ProviderDaoOpenHelper;
import com.android.canvasproject.model.entity.Canvas;
import com.android.canvasproject.view.utils.ComponentViewUtil;
import com.android.canvasproject.view.utils.ConstantsAction;

public class MyCanvasFragment extends Fragment {

	private LinearLayout myCanvasLayout;
	private View viewContext;
	private Context context;
	public static LinearLayout blockProjectsLayout;
	public static LinearLayout blockBusinessLayout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		viewContext = inflater.inflate(R.layout.fragment_my_canvas, container, false);
		context = viewContext.getContext();

		myCanvasLayout = (LinearLayout) viewContext.findViewById(R.id.mycanvas_layout);

		//TODO teste programação dashboard [Project Model Canvas]
		TextView projCanvasLabel = ComponentViewUtil.getTextView(
				context, getResources().getString(R.string.project_model_canvas),
				R.style.textview_titles_dashboard);
		projCanvasLabel.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		projCanvasLabel.setBackgroundResource(R.drawable.layout_list_background_container_projects);

		blockProjectsLayout = ComponentViewUtil.getLinearLayout(context, LinearLayout.HORIZONTAL);
		blockProjectsLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		HorizontalScrollView scrollProject = new HorizontalScrollView(context);
		scrollProject.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		scrollProject.addView(blockProjectsLayout);
		scrollProject.setPadding(10, 0, 10, 50);
		scrollProject.setHorizontalScrollBarEnabled(false);
		scrollProject.setOverScrollMode(View.OVER_SCROLL_IF_CONTENT_SCROLLS);

		LinearLayout blockProjectModelLayout = ComponentViewUtil.getLinearLayout(context, LinearLayout.VERTICAL);
		blockProjectModelLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

		blockProjectModelLayout.addView(projCanvasLabel);
		blockProjectModelLayout.addView(scrollProject);
		
		
		//TODO teste programação dashboard [Business Model Canvas]
		TextView businessCanvasLabel = ComponentViewUtil.getTextView(
				context, getResources().getString(R.string.business_model_canvas),
				R.style.textview_titles_dashboard);
		businessCanvasLabel.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		businessCanvasLabel.setBackgroundResource(R.drawable.layout_list_background_container_projects);

		blockBusinessLayout = ComponentViewUtil.getLinearLayout(context, LinearLayout.HORIZONTAL);
		blockBusinessLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		HorizontalScrollView scrollBusiness = new HorizontalScrollView(context);
		scrollBusiness.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		scrollBusiness.setPadding(10, 0, 10, 50);
		scrollBusiness.setHorizontalScrollBarEnabled(false);
		scrollBusiness.addView(blockBusinessLayout);

		LinearLayout blockBusinessModelLayout = ComponentViewUtil.getLinearLayout(context, LinearLayout.VERTICAL);
		blockBusinessModelLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

		blockBusinessModelLayout.addView(businessCanvasLabel);
		blockBusinessModelLayout.addView(scrollBusiness);
		
		try {
			loadCanvas();

		} catch (java.lang.InstantiationException e) {
			Log.e(MyCanvasFragment.class.getName(), ConstantsAction.Error.INSTANCE_CONTEXT, e);
			throw new InstantiationException(e.getMessage(), e);
		}

		myCanvasLayout.setPadding(10, 10, 10, 10);
		myCanvasLayout.addView(blockProjectModelLayout);
		myCanvasLayout.addView(blockBusinessModelLayout);
		
		return viewContext;
	}
	
	public int loadCanvas() throws java.lang.InstantiationException {
		List<Canvas> canvas = new ArrayList<Canvas>();
		ProviderDaoOpenHelper provider = new ProviderDaoOpenHelper(getActivity());

		try {
			List<Map<String, Object>> result = provider.findAll(Canvas.CanvasDao.TB_CANVAS, Canvas.CanvasDao.COLS_CANVAS);
			if(result != null && !result.isEmpty()){
				for(Map<String, Object> map : result){
					Canvas c = Canvas.entityStructure(map);

					if(c.getCanvasType().equals(CanvasTypeEnum.PROJECT_MODEL_CANVAS))
						blockProjectsLayout.addView(ComponentViewUtil.createNewCanvas(getActivity(), c.getTitle(), c.getDetail(), c.getCanvasType(), c));

					else if(c.getCanvasType().equals(CanvasTypeEnum.BUSINESS_MODEL_CANVAS))
						blockBusinessLayout.addView(ComponentViewUtil.createNewCanvas(getActivity(), c.getTitle(), c.getDetail(), c.getCanvasType(), c));

					canvas.add(c);
				}
			}

		} catch (SQLException e) {
			Log.e(Canvas.CanvasDao.TB_CANVAS, ConstantsAction.Database.QUERY, e);

		} finally {
			provider.cleanup();

		}

		return canvas.size();
	}
}
